package reading.common.exception;

import freemarker.core.Environment;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;

import java.io.IOException;
import java.io.Writer;

/**
 * @author TXW
 * @version V1.0
 * @Title: ${file_name}
 * @date 2018/9/18 17:59
 */
public class TemplateExceptionHand implements TemplateExceptionHandler {

    @Override
    public void handleTemplateException(TemplateException te, Environment env, Writer out) throws TemplateException {
        String missingVariable = "undefined";
        try {
            String[] tmp = te.getMessageWithoutStackTop().split("\n");
            if (tmp.length > 1) {
                tmp = tmp[1].split(" ");
            }
            if (tmp.length > 1) {
                missingVariable = tmp[1];
            }

            out.write("[系统繁忙，请稍后再试！]");

        } catch (IOException e) {
            throw new TemplateException("Failed to print error message. Cause: " , env);
        }

    }
}
